﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BUS;

public partial class User_NhapHang : System.Web.UI.Page
{
    DataTable dtTenSP;
    DataTable dtTenNCC;
    float tongtien;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
            Response.Redirect("../Default.aspx");

        if (!IsPostBack)
        {
            cmbTenSP.DataSource = dtTenSP = NhapHangBUS.LayDanhSachTenSP();
            cmbTenSP.DataValueField = "TenSP";
            cmbTenSP.DataBind();
            cmbTenNCC.DataSource = dtTenNCC = NhapHangBUS.LayDanhSachTenNCC();
            cmbTenNCC.DataValueField = "TenNCC";
            cmbTenNCC.DataBind();
            bindgrid();
            if (float.Parse(lbTongTien.Text) == 0)
            {
                btnNhapHang.Visible = false;
                Label2.Visible = false;
            }
        }
    }
    protected void bindgrid()
    {
        ListSanPham listsp;
        listsp = (ListSanPham)Session["NhapHang"];
        if (listsp == null)
            listsp = new ListSanPham();
        gvSanPham.DataSource = listsp.sp;
        gvSanPham.DataBind();
        for (int i = 0; i < gvSanPham.Rows.Count; i++)
        {
            tongtien = tongtien + (float.Parse(((TextBox)this.gvSanPham.Rows[i].Cells[3].FindControl("gv_soluong")).Text) * float.Parse(((TextBox)this.gvSanPham.Rows[i].Cells[4].FindControl("gv_dongia")).Text));
        }
        if (tongtien > 0)
        {
            btnNhapHang.Visible = true;
        }
        lbTongTien.Text = tongtien.ToString();
    }
    protected void btnThemSP_Click(object sender, EventArgs e)
    {
        ListSanPham listsp;
        listsp = (ListSanPham)Session["NhapHang"];
        if (listsp == null)
            listsp = new ListSanPham();

        SanPham_Nhap sp = new SanPham_Nhap();
        sp.masp = NhapHangBUS.LayMaSP(cmbTenSP.Text);
        sp.tensp = cmbTenSP.Text;
        sp.dongia = float.Parse(txtDonGia.Text);
        sp.soluong = float.Parse(txtSoLuong.Text);
        sp.tenncc = cmbTenNCC.Text;

        listsp.ThemSP(sp);
        Session["NhapHang"] = listsp;
        btnNhapHang.Visible = true;
        Label2.Visible = true;
        bindgrid();
    }
    protected void btnNhapHang_Click(object sender, EventArgs e)
    {
        String manv = (String)Session["UserName"];
        PhieuNhapHang ph = new PhieuNhapHang();
        ListSanPham list = (ListSanPham)Session["NhapHang"];
        float tongtien = 0;
        for (int i = 0; i < list.sp.Count; i++)
        {
            tongtien = tongtien + (list.sp[i].soluong * list.sp[i].dongia);
        }
        ph.tennv = NhanVienBUS.LayTenNhanVien(manv);
        ph.tongtien = tongtien;
        ph.ngaynhap = DateTime.Now;

        NhapHangBUS.NhapHang(ph, list);
    }
    protected void gvSanPham_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow row = gvSanPham.Rows[Int32.Parse(e.CommandArgument.ToString())];
        String masp;
        String tenncc;
        float soluong;
        float dongia;
        if (e.CommandName.Equals("CapNhat"))
        {
            masp = row.Cells[0].Text;
            tenncc = HttpUtility.HtmlDecode(row.Cells[2].Text);
            String sl = ((TextBox)row.Cells[3].FindControl("gv_soluong")).Text.Trim();
            String dg = ((TextBox)row.Cells[4].FindControl("gv_dongia")).Text.Trim();
            if (sl != "" && dg != "")
            {
                soluong = float.Parse(sl);
                dongia = float.Parse(dg);
                if (soluong > 0 && dongia > 0)
                {
                    ListSanPham listsp;
                    listsp = (ListSanPham)Session["NhapHang"];
                    listsp.CapNhatSP(masp, tenncc, soluong, dongia);
                    Session["NhapHang"] = listsp;
                    bindgrid();
                    Label1.Visible = false;
                }
                else
                {
                    Label1.Text = "Số lượng và đơn giá phải lớn hơn 0";
                    Label1.Visible = true;
                }
            }
            else
            {
                Label1.Text = "Xin nhập số lượng và đơn giá mới";
                Label1.Visible = true;
            }
        }
        gotoAnchorScript("list");
    }

    private void gotoAnchorScript(string anchor)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#" + anchor + "';", true);
    }
    
    protected void gvSanPham_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        String masp;
        String tenncc;
        GridViewRow row = gvSanPham.Rows[e.RowIndex];
        masp = row.Cells[0].Text;
        tenncc = HttpUtility.HtmlDecode(row.Cells[2].Text);
        ListSanPham listsp;
        listsp = (ListSanPham)Session["NhapHang"];
        listsp.XoaSP(masp, tenncc);
        Session["NhapHang"] = listsp;
        bindgrid();
        gotoAnchorScript("list");
    }
}
